package com.taldh.design.patterns.factories.simple;

import com.google.common.base.Supplier;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

/**
 * date: 2020/5/24
 *
 * @author 袁小黑
 * @version 1.0.0
 */

public class WeaponFactory {

//    public Weapon getWeapon(WeaponType weaponType) {
//        switch (weaponType) {
//            case AXE:
//                return new Axe();
//            case BOW:
//                return new Bow();
//            case SPEAR:
//                return new Spear();
//            case SWORD:
//                return new Sword();
//            default:
//                break;
//        }
//        return null;
//    }


    //以上为解法1，以下为解法2，这种解法类似spring的ioc容器（两者还是有较大区别，只是都用了ConcurrentHashMap）。
    private final static ConcurrentHashMap<WeaponType, Supplier<Weapon>> map = new ConcurrentHashMap<>();
    static {
        map.put(WeaponType.AXE, Axe::new);
        map.put(WeaponType.BOW, Bow::new);
        map.put(WeaponType.SPEAR, Spear::new);
        map.put(WeaponType.SWORD, Sword::new);
    }

    public Weapon getWeapon(WeaponType weaponType) {
        return map.get(weaponType).get();
    }

}